<template>
    <div id="cross-bar" ref="canvas" class="canvas" :style="{width: '100%', height: canHeight + 'px'}"></div>
</template>
<script setup lang="ts">
import { defineProps, computed, onMounted } from 'vue'
// import { createPinia } from 'pinia'
import * as echarts from 'echarts/core';
import {
  TitleComponent,
  type TitleComponentOption,
  TooltipComponent,
  type TooltipComponentOption,
  GridComponent,
  type GridComponentOption,
  LegendComponent,
  type LegendComponentOption
} from 'echarts/components';
import { BarChart, type BarSeriesOption } from 'echarts/charts';
import { CanvasRenderer } from 'echarts/renderers';
// import { useChartsStore } from '@/stores/charts';
// const pinia = createPinia();
// const chartStore = useChartsStore(pinia)
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  BarChart,
  CanvasRenderer
]);

type EChartsOption = echarts.ComposeOption<
  | TitleComponentOption
  | TooltipComponentOption
  | GridComponentOption
  | LegendComponentOption
  | BarSeriesOption
>;
const props = defineProps({
    canvasHeight: {
        type:Number,
        default: 300
    },
    data: {
        type: Object,
        default: () => { } 
    }
})
let canHeight = computed(() => { return props.canvasHeight })
onMounted(() => {
    var chartDom = document.getElementById('cross-bar')!;
    var myChart = echarts.init(chartDom);
    var option: EChartsOption;

    option = {
        title: {
            text: ''
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
            type: 'shadow'
            }
        },
        legend: {
            show: true,
            top: 20,
            right: 40,
            textStyle: { color: '#11cb92' },
        },
        grid: {
            top: 60,
            left: 20,
            right: 80,
            bottom: 20,
            containLabel: true
        },
        xAxis: {
            type: 'value',
            boundaryGap: [0, 0.01],
            axisLabel: { show: true, color: '#c3c6ce'},
            axisLine:{ show:true, lineStyle: {color: '#4a879a'} },
            splitLine: {show: true,lineStyle: {color: '#4a879a'}},
        },
        yAxis: {
            type: 'category',
            axisLabel: { show: true, color: '#c3c6ce'},
            data: props.data.xAxisDataList
        },
        series: [
            {
                name: '2011',
                type: 'bar',
                itemStyle: {
                    color: {
                        x: 0,
                        y: 0,
                        x2: 1,
                        y2: 0,
                        colorStops: [{
                            offset: 0, color: '#4766f8' // 0% 处的颜色
                        }, {
                            offset: 1, color: '#07eaf9' // 100% 处的颜色
                        }],
                        global: false // 缺省为 false
                    },
                    borderRadius: [0, 4, 4, 0]
                },
                label: { show: true, color: '#ffffff', align: 'left',
                    backgroundColor: '#4766f8'
                },
                data: props.data.seriesDataList
            }
        ]
    };
    option && myChart.setOption(option);
})
</script>

<style scope>
</style>
